package com.zc.exception;

import com.alibaba.fastjson.JSON;
import com.zc.base.ResponseResult;
import com.zc.utils.WebUtil;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @description: 授权失败：它会封装AccessDeniedException，然后调用AccessDeniedHandler的handle方法处理
 * @author: cong
 * @create: 2024/8/7
 */
@Component
public class AccessDeniedHandlerImpl implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
        ResponseResult<Object> responseResult = new ResponseResult<>(HttpStatus.FORBIDDEN.value(), "权限不足", null);
        WebUtil.renderString(response, JSON.toJSONString(responseResult));
    }
}
